import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:yinshui_caiying/utils/colors.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yinshui_caiying/widgets/border_dashed.dart';

///数字增加器
///[title]左侧标题,*必填
///
///[placeholder]输入提示
///
///[step]增加步数
///
///[controller]输入框控制器，当初始化时要反填值时用
///
///[onChanged]值改变的回调函数
///
///[inputFormatters]格式化函数
///
///
///[request]是否必填，是的话表单验证时会以弹窗的形式提示placeholder的内容
///
///[validator]验证规则，想用自定义验证规则的话，就传入
///
///[hasBorder]是否展示下划线，默认true
class FormNumber extends StatelessWidget {
  String title;
  String placeholder;
  num step;
  TextEditingController? controller;
  void Function(String)? onChanged;
  List<TextInputFormatter>? inputFormatters;
  String? Function(String?)? validator;
  bool hasBorder;
  bool request;

  FocusNode focusNode = FocusNode();
  final textLength = 0.obs;
  FormNumber(
      {required this.title,
      this.placeholder = '请输入内容',
      this.step = 1,
      this.controller,
      this.onChanged,
      this.inputFormatters,
      this.validator,
      this.hasBorder = true,
      this.request = false});

  @override
  Widget build(BuildContext context) {
    if (controller == null) {
      controller = TextEditingController.fromValue(TextEditingValue(
          text: '1',
          selection: TextSelection.fromPosition(TextPosition(
              affinity: TextAffinity.downstream, offset: textLength.value))));
    }
    controller!.addListener(() {
      textLength.value = controller!.text.length;
    });
    focusNode.addListener(() {
      if (focusNode.hasFocus) {
        //有焦点
        controller!.selection = TextSelection.fromPosition(TextPosition(
            affinity: TextAffinity.downstream, offset: textLength.value));
      } else {
        //失去焦点
      }
    });
    return GestureDetector(
        onTap: () {
          FocusScope.of(context).requestFocus(focusNode);
        },
        child: Column(children: [
          Container(
              height: 147.h,
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      title,
                      style: TextStyle(
                        fontSize: 40.sp,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                    Expanded(
                        flex: 1,
                        child: Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              MaterialButton(
                                  minWidth: 44.w,
                                  height: 44.h,
                                  padding: EdgeInsets.all(0),
                                  color: MyColors.main,
                                  onPressed: () {
                                    num inputValue =
                                        int.parse(controller!.text) - step;
                                    controller!.text = inputValue.toString();
                                    onChanged!(inputValue.toString());
                                  },
                                  child: Icon(
                                    Icons.remove,
                                    size: 42.sp,
                                    color: Colors.white,
                                  ),
                                  shape: CircleBorder()),
                              Container(
                                width: 80.w,
                                alignment: Alignment.centerRight,
                                child: TextFormField(
                                    focusNode: focusNode,
                                    keyboardType: TextInputType.number,
                                    inputFormatters: inputFormatters,
                                    onChanged: onChanged,
                                    controller: controller,
                                    textAlign: TextAlign.center,
                                    textAlignVertical: TextAlignVertical.top,
                                    style: TextStyle(
                                      color: Color.fromRGBO(193, 192, 192, 1),
                                      fontSize: 40.sp,
                                      fontWeight: FontWeight.w400,
                                    ),
                                    decoration: InputDecoration(
                                        hintText: placeholder,
                                        hintStyle: TextStyle(
                                            color: Color.fromRGBO(
                                                193, 192, 192, 1)),
                                        border: InputBorder.none,
                                        isDense: true,
                                        errorStyle: TextStyle(height: 0)),
                                    validator: this.request
                                        ? (value) {
                                            if (value == '') {
                                              SmartDialog.showToast(
                                                  this.placeholder,
                                                  alignment: Alignment.center);
                                              return '';
                                            } else {
                                              return null;
                                            }
                                          }
                                        : null),
                              ),
                              MaterialButton(
                                  minWidth: 44.w,
                                  height: 44.h,
                                  padding: EdgeInsets.all(0),
                                  color: MyColors.main,
                                  onPressed: () {
                                    num inputValue =
                                        int.parse(controller!.text) + step;
                                    onChanged!(inputValue.toString());
                                    controller!.text = inputValue.toString();
                                  },
                                  child: Icon(
                                    Icons.add,
                                    size: 42.sp,
                                    color: Colors.white,
                                  ),
                                  shape: CircleBorder())
                            ]))
                  ])),
          hasBorder ? MySeparator() : Container()
        ]));
  }
}
